﻿
Imports std
Imports storoomc
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl

Public Class recentElement
    Implements ICloneable, IComparable(Of recentElement)

    Public name As String = Nothing
    Public user As String = Nothing
    Public time As String = Nothing

    Public Sub New()
    End Sub

    Public Sub New(ByVal name As String, ByVal user As String, ByVal time As String)
        copy(Me.name, name)
        copy(Me.user, user)
        copy(Me.time, time)
    End Sub

    Public Function Clone() As Object Implements ICloneable.Clone
        Return New recentElement(name, user, time)
    End Function

    Public Function CompareTo(ByVal other As recentElement) As Int32 Implements IComparable(Of recentElement).CompareTo
        If other Is Nothing Then
            Return 1
        Else
            Dim cmp As Int32
            cmp = compare(name, other.name)
            If cmp = 0 Then
                cmp = compare(user, other.user)
            End If
            Return cmp
        End If
    End Function
End Class

Public MustInherit Class recent
    Inherits recent(Of recentElement)

    Public Const containerId As String = "re_c"
    Public Const separator As Char = character.tab

    Protected Overloads Sub initial(ByVal config As configParser, _
                                    ByVal configName As String, ByVal defaultValue As Int64)
        MyBase.initial(config)
        assert(Not config Is Nothing, "recent needs a valid config to initialize.")
        initial(config.parameter(configNaming.wfileis.name, configName, defaultValue))
    End Sub

    Protected MustOverride Function titleText() As String
    Protected MustOverride Function norecentText() As String
    Protected MustOverride Function generateNamelink(ByVal s As String) As String
    Protected MustOverride Function key() As String

    Private Delegate Function processElementDelegate(ByVal r As recentElement) As div

    Private Class list
        Inherits storoomc.list

        Protected Overrides Function enableModify() As Boolean
            Return True
        End Function

        Protected Overrides Function enableRead() As Boolean
            Return True
        End Function

        Protected Overrides Function enableUniqueWrite() As Boolean
            Return False
        End Function

        Protected Overrides Function enableWrite() As Boolean
            Return False
        End Function

        Protected Overrides Function keyprefix() As String
            Return "recent"
        End Function
    End Class

    Protected Overrides Sub read()
        Dim nl As list = Nothing
        nl = New list()
        nl.read(key())

        Dim it As list.iterator = Nothing
        it = nl.begin()
        While it <> nl.end
            Dim words As vector(Of String) = Nothing
            strsepar((+it), separator, words, False)
            insert(nothingToEmptyString(words(0)), nothingToEmptyString(words(1)), nothingToEmptyString(words(2)))
            it += 1
        End While
    End Sub

    Protected Overrides Sub write(ByVal l As vector(Of recentElement))
        If Not l Is Nothing Then
            Dim nl As list = Nothing
            nl = New list()

            For i = 0 To l.size() - 1
                nl.push_back(escape_unref(l(i).name) + separator + escape_unref(l(i).user) + _
                             separator + escape_unref(l(i).time))
            Next

            nl.modify(key())
        End If
    End Sub

    Private Function processBriefElement(ByVal context As httpContext, ByVal r As recentElement) As div
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not r Is Nothing, "r is nothing.")
        Dim rtn As div = Nothing
        rtn = context.createControl(New div())

        Dim a As a = Nothing
        a = context.createControl(New a())
        a.href = generateNamelink(r.name)
        a.text = r.name
        rtn.addchild(a)

        Dim span As span = Nothing
        span = context.createControl(New span())
        span.text = " - " + r.time
        rtn.addchild(span)

        Return rtn
    End Function

    Private Function processBriefElement(ByVal r As recentElement) As div
        Return processBriefElement(httpServer2.currentContext(), r)
    End Function

    Private Function processFullElement(ByVal context As httpContext, ByVal r As recentElement) As div
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not r Is Nothing, "r is nothing.")
        Dim rtn As div = Nothing
        rtn = context.createControl(New div())

        Dim a As a = Nothing
        a = context.createControl(New a())
        a.href = generateNamelink(r.name)
        a.text = r.name
        rtn.addchild(a)

        Dim span As span = Nothing
        span = context.createControl(New span())
        span.text = " - "
        rtn.addchild(span)

        Dim id As idshow = Nothing
        id = context.createControl(New idshow())
        id.setData(r.user)
        rtn.addchild(id)

        span = context.createControl(New span())
        span.text = " - " + r.time
        rtn.addchild(span)

        Return rtn
    End Function

    Private Function processFullElement(ByVal r As recentElement) As div
        Return processFullElement(httpServer2.currentContext(), r)
    End Function

    Private Function processList(ByVal context As httpContext, ByVal root As xmlcontrol, _
                                 ByVal l As vector(Of recentElement), ByVal pe As processElementDelegate, _
                                 ByVal first As Int64, ByVal count As Int64) As Boolean
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not root Is Nothing, "root is nothing.")

        If l Is Nothing OrElse l.size() = 0 Then
            Dim span As span = Nothing
            span = context.createControl(New span())
            span.text = norecentText()

            root.addchild(span)
        Else
            If first < 0 Then
                first = 0
            End If
            If count < 0 OrElse count > l.size() - first Then
                count = l.size() - first
            End If
            Dim i As Int64
            For i = first + count - 1 To first Step -1
                If Not l(i) Is Nothing Then
                    root.addchild(pe(l(i)))
                End If
            Next
        End If

        Return True
    End Function

    Private Function processList(ByVal root As xmlcontrol, ByVal l As vector(Of recentElement), _
                                 ByVal pe As processElementDelegate, _
                                 ByVal first As Int64, ByVal count As Int64) As Boolean
        Return processList(httpServer2.currentContext(), root, l, pe, first, count)
    End Function

    Protected Overloads Function processImpl(ByVal context As httpContext, _
                                             ByRef control As xmlcontrol, _
                                             ByVal l As vector(Of recentElement)) As Boolean
        assert(Not context Is Nothing, "context is nothing.")
        Dim nopage As Boolean = Nothing
        nopage = context.url().queryExist(query.nopage)

        Dim first As Int64
        first = context.url().queryInt64(query.first)
        Dim count As Int64
        count = context.url().queryInt64(query.count)
        Dim last As Boolean
        last = context.url().queryExist(query.last)

        If last Then
            first = l.size() - count
        End If
        If nopage Then
            Dim container As container = Nothing
            container = context.createControl(New container())
            control = container
            Return processList(container, l, AddressOf processBriefElement, first, count)
        Else
            Dim page As xhtmlpage = Nothing
            page = context.createControl(New xhtmlpage())
            page.addcsslink(cssrequest.generateCssfileLinkUrl("recent.css"))
            page.title().text = titleText()

            page.body().addchild(header.generateInstance(page))

            Dim div As div = Nothing
            div = context.createControl(New div())
            div.id = containerId
            page.body().addchild(div)

            Dim h3 As h3 = Nothing
            h3 = context.createControl(New h3())
            h3.text = titleText()
            div.addchild(h3)

            Dim listContainer As div = Nothing
            listContainer = context.createControl(New div())
            div.addchild(listContainer)

            control = page
            Return processList(listContainer, l, AddressOf processFullElement, first, count)
        End If
    End Function

    Protected NotOverridable Overrides Function processImpl(ByRef control As xmlcontrol, _
                                                            ByVal l As vector(Of recentElement)) As Boolean
        Return processImpl(httpServer2.currentContext(), control, l)
    End Function

    Public Shadows Sub insert(ByVal name As String, ByVal user As String, ByVal time As String)
        MyBase.insert(New recentElement(name, user, time))
    End Sub

    Public Shared Function generateAjaxLink(ByVal baseurl As String, _
                                            Optional ByVal first As Int64 = 0, _
                                            Optional ByVal count As Int64 = npos, _
                                            Optional ByVal last As Boolean = False) As String
        Dim rtn As String = Nothing
        copy(rtn, baseurl)
        rtn = urlUtils.appendQuery(rtn, query.nopage, "1")
        If first > 0 Then
            rtn = urlUtils.appendQuery(rtn, query.first, Convert.ToString(first))
        End If
        If count > 0 Then
            rtn = urlUtils.appendQuery(rtn, query.count, Convert.ToString(count))
        End If
        If last Then
            rtn = urlUtils.appendQuery(rtn, query.last, "1")
        End If
        Return rtn
    End Function
End Class

Public Class recentTopic
    Inherits recent

    Public Const _pathidentity As String = "/rect"

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Public Shared Function generateRecentTopicAjaxLink(Optional ByVal first As Int64 = 0, _
                                                       Optional ByVal count As Int64 = npos, _
                                                       Optional ByVal last As Boolean = False) As String
        Return generateAjaxLink(strmid(_pathidentity, strlen(network.pathSeperator)), first, count, last)
    End Function

    Public Overrides Sub initial(Optional ByVal config As configParser = Nothing)
        MyBase.initial(config)
        MyBase.initial(config, configNaming.wfileis.recentTopicCount, _
                       configNaming.defaultValue.wfileis.recentTopicCount)
    End Sub

    Protected Overloads Function titleText(ByVal context As httpContext) As String
        assert(Not context Is Nothing, "context is nothing.")
        Return context.string(stringId.RECENT_TOPIC_TITLE)
    End Function

    Protected Overrides Function titleText() As String
        Return titleText(httpServer2.currentContext())
    End Function

    Protected Overrides Function generateNamelink(ByVal s As String) As String
        Return view.generateViewlink(s)
    End Function

    Protected Overloads Function norecentText(ByVal context As httpContext) As String
        assert(Not context Is Nothing, "context is nothing.")
        Return context.string(stringId.RECENT_TOPIC_NORECENT)
    End Function

    Protected Overrides Function norecentText() As String
        Return norecentText(httpServer2.currentContext())
    End Function

    Public Overloads Function processImpl(ByVal context As httpContext, ByRef control As xmlcontrol) As Boolean
        'force with page output
        context.url().query.erase(query.nopage)
        Return MyBase.processImpl(context, control)
    End Function

    Protected Overrides Function key() As String
        Return "topic"
    End Function
End Class

Public Class recentCategory
    Inherits recent

    Public Const _pathidentity As String = "/recc"

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Public Shared Function generateRecentCategoryAjaxLink(Optional ByVal first As Int64 = 0, _
                                                          Optional ByVal count As Int64 = npos, _
                                                          Optional ByVal last As Boolean = False) As String
        Return generateAjaxLink(strmid(_pathidentity, strlen(network.pathSeperator)), first, count, last)
    End Function

    Public Overrides Sub initial(Optional ByVal config As configParser = Nothing)
        MyBase.initial(config)
        MyBase.initial(config, configNaming.wfileis.recentCategoryCount, _
                       configNaming.defaultValue.wfileis.recentCategoryCount)
    End Sub

    Protected Overloads Function titleText(ByVal context As httpContext) As String
        assert(Not context Is Nothing, "context is nothing.")
        Return context.string(stringId.RECENT_CATEGORY_TITLE)
    End Function

    Protected Overrides Function titleText() As String
        Return titleText(httpServer2.currentContext())
    End Function

    Protected Overrides Function generateNamelink(ByVal s As String) As String
        Return catView.generateCatViewlink(s)
    End Function

    Protected Overloads Function norecentText(ByVal context As httpContext) As String
        assert(Not context Is Nothing, "context is nothing.")
        Return context.string(stringId.RECENT_CATEGORY_NORECENT)
    End Function

    Protected Overrides Function norecentText() As String
        Return norecentText(httpServer2.currentContext())
    End Function

    Protected Overrides Function key() As String
        Return "category"
    End Function
End Class